<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Blog extends MY_Controller {

	function __construct() {
		parent::__construct();

		$this->load->helper('url');
		$this->load->helper('form');

		//$this->output->cache(0.5);


	}

	public function index() {
		$data['title'] = "MyHome - 我的家园";
		$data['heading'] = "MyHome";
		//load->library
		$this->load->library('pagination');

		//load->model
		$this->load->model('MY_Blog');
		$this->load->model('MTerm');

		//$data
		$config['per_page'] = '2';
		$config['total_rows'] = $this->MY_Blog->count_all();
		$offset = 0;
		if($this->uri->segment(2)) {
			$offset = $this->uri->segment(2);
			$data['title'] = "Page  " . ($offset / $config['per_page'] + 1) . "/" .
					 ceil($config['total_rows'] / $config['per_page']) . " - " .
					 $data['title'];
		}
		$data['blogs'] = $this->MY_Blog->get($config['per_page'], $offset);
		$data['title'] = "首页 - " . $data['title'];
		foreach($data['blogs'] as $row) {
			$category_row = $this->MTerm->get_category_link($row->pid);
			if($category_row) {
				$row->term_id = $category_row->term_id;
				$row->term_name = $category_row->term_name;
			}
			$row->term_id = 1;
			$row->term_name = '未分类';
		}
		//pagination
		$config['base_url'] = site_url('page');
		$config['uri_segment'] = 2;
		$this->pagination->initialize($config);
		$data['page'] = $this->pagination->create_links();

		//load->view
		$this->load->view('header', $data);
		$this->load->view('blog_view', $data);
		$this->load->view('sidebar');
		$this->load->view('footer');
	}

	public function post() {
		$pid = $this->uri->segment(2);
		$data['title'] = "MyHome - 我的家园";
		$data['heading'] = "MyHome";
		//load->model
		$this->load->model('Post');
		$this->load->model('Comment');
		$this->load->model('MTerm');

		$this->Post->update_veiws($pid);
		//$data


		$data['post_row'] = $this->Post->get_post($pid);
		if(! $data['post_row']) {
			show_404();
		}

		$category_row = $this->MTerm->get_category_id($pid);
		if($category_row) {
			$data['post_row']->term_id = $category_row->term_id;
			//$data['post_row']->term_name = $category_row->term_name;
		}
		$data['post_row']->term_id = 1;
		$data['post_row']->term_name = '未分类1';

		//$data['category_row'] = $category_row;
		$data['next'] = $this->Post->get_title($pid + 1);
		$data['prev'] = $this->Post->get_title($pid - 1);

		$data['comments_rows'] = $this->Comment->get_comments($pid);

		$data['title'] = $data['post_row']->post_title . " - " . $data['title'];
		$data['attributes'] = array('id'=>'comment');
		//load->view
		$this->load->view('header', $data);
		$this->load->view('post_view', $data);
		$this->load->view('comment_view', $data);
		$this->load->view('sidebar');
		$this->load->view('footer');
	}

}

/* End of file blog.php */
/* Location: ./application/controllers/blog.php */